<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px;">
          <!-- 头部区域 -->
          <el-col :span="20">{{ company.name }}</el-col>
          <el-col :span="4">
            <el-row type="flex">
              <el-col>负责人</el-col>
              <el-col>
                <!-- 下拉菜单 -->
                <el-dropdown @command="addDeparts">
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="add">
                      添加部门
                    </el-dropdown-item>
                    <el-dropdown-item>
                      修改部门
                    </el-dropdown-item>
                    <el-dropdown-item>
                      删除部门
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树状结构 -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <template #default="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%;">
              <el-col :span="20">{{ data.name }}</el-col>
              <el-col :span="4">
                <el-row type="flex">
                  <el-col>负责人</el-col>
                  <el-col>
                    <!-- 下拉菜单 -->
                    <el-dropdown @command="handleCommand">
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item :command="{type:'add',singleData:data}">
                          添加部门
                        </el-dropdown-item>
                        <el-dropdown-item :command="{type:'edit',singleData:data}">
                          修改部门
                        </el-dropdown-item>
                        <el-dropdown-item :command="{type:'del',id:data.id}">
                          删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <addDept ref="addDept" :show-dialog.sync="showDialog" :departs-list="departsList" :node-data="nodeData" @addSuccess="getDepartmentList" />
  </div>
</template>

<script>
import { getDepartmentListApi, delDepartmentApi } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'
// 引入弹层组件
import addDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  components: {
    addDept
  },
  data() {
    return {
      company: {
        id: '',
        name: ''
      },
      departs: [
      ],
      // 存储平铺的所有项目数组
      departsList: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      nodeData: {},
      loading: true
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    /**
     * 获取组织架构列表
     */
    async getDepartmentList() {
      this.loading = true
      const res = await getDepartmentListApi()
      this.departsList = JSON.parse(JSON.stringify(res.data.depts))
      this.company.name = res.data.companyName
      this.departs = transListToTreeData(res.data.depts, '')
      this.loading = false
    },
    // 下拉框的事件
    handleCommand({ type, id, singleData }) {
      if (type === 'add') {
        this.openDialog(singleData)
        // 使用子组件内获取负责人的方法
        this.$refs.addDept.getDepartmentManagerList()
      }
      if (type === 'edit') {
        this.editDepartment(singleData)
      }
      if (type === 'del') {
        this.delDepartment(id)
      }
    },
    /**
     * 删除部门
     * @param id 部门id
     */
    delDepartment(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartmentApi(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 重新获取数据
        this.getDepartmentList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /**
     * 添加部门
     */
    openDialog(singleData) {
      this.showDialog = true
      this.nodeData = singleData
    },
    closeDialog() {
      this.showDialog = false
    },
    /**
     * 添加一级公司部门
     * @param command dropdown数据
     */
    addDeparts(command) {
      if (command === 'add') {
        this.showDialog = true
        const newObj = {
          children: this.departs,
          id: ''
        }
        this.nodeData = newObj
        // 使用子组件内获取负责人的方法
        this.$refs.addDept.getDepartmentManagerList()
      }
    },
    editDepartment(singleData) {
      this.showDialog = true
      // 获取数据
      this.$refs.addDept.getDepartmentDetail(singleData.id)
      // 使用子组件内获取负责人的方法
      this.$refs.addDept.getDepartmentManagerList()
    }

  }
}
</script>

<style>
.tree-card {
  padding: 30px 30px;
  font-size: 14px;
}
/* .el-tree { */
  ::v-deep {
    /* // 小三角的样式, 去掉默认的小三角的旋转效果 */
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    /* // 有子节点 且未展开 + */
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    /* // 有子节点 且已展开 - */
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    /* // 没有子节点 */
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
/* } */
</style>
